package main

//group页面
import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func getUserInfo(c *gin.Context) {
	fmt.Println("执行getInfo接口")
	c.JSON(http.StatusOK, gin.H{
		"msg": "success",
	})
}

// 接口执行前后的操作
func useHandler1(c *gin.Context) {
	token := c.Request.Header.Get("x-token")
	fmt.Printf("打印token %v", token)
	start := time.Now()
	c.Next()
	//组织接口执行, 执行返回
	//c.Abort()
	//return
	end := time.Since(start)
	fmt.Printf("请求消耗时间: %v\n", end)
}

func main() {
	r := gin.Default()

	r.Use(useHandler1)

	userGroup := r.Group("/user")

	userGroup.GET("getInfo", getUserInfo)

	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "无此页面",
		})
	})

	r.Run(":8090")

}
